'use strict';

imsiMacApp.controller('ImsiMacCtrl', ['$scope', '$http', function($scope, $http){
    console.log('in ImsiMacCtrl');
    $scope.search_str = '';
    $scope.results = [];
    $scope.selected = 'IMSI';
    $scope.select_imsi = function(){
        $scope.selected = 'IMSI';
    };
    $scope.select_mac = function(){
        $scope.selected = 'MAC';
    };
    $scope.select = function(){
        // console.log($scope.search_str)
        if($scope.search_str == null || $scope.search_str.trim() == ''){
            return
        }
        if($scope.selected == 'IMSI'){
            $scope.select_by_imsi();
        }
        else if($scope.selected == 'MAC'){
            $scope.select_by_mac();
        }
    };
    $scope.find = function(){
        $scope.results='';
        // console.log($scope.search_str)
        if($scope.selected == 'IMSI'){
            $scope.find_by_imsi()
        }
        else if($scope.selected == 'MAC'){
            $scope.find_by_mac()
        }
    };

    $scope.get_results_succeed = function(response){
        console.log(response.data);
        $scope.results = response.data
    };

    $scope.get_results_failed = function(response){
        console.log(response);
        alert(response)
    };

    $scope.select_by_imsi = function(){
        console.log($scope.search_str.trim());
        $http.post('/api/select_by_imsi', {'imsi': $scope.search_str.trim()})
            .then($scope.get_results_succeed, $scope.get_results_failed)

    };

    $scope.select_by_mac = function(){
        console.log($scope.search_str.trim());
        $http.post('/api/select_by_mac', {'mac': $scope.search_str.trim()})
            .then($scope.get_results_succeed, $scope.get_results_failed)

    };

    $scope.find_by_imsi = function(){
        console.log($scope.search_str.trim());
        $http.post('/api/find_by_imsi', {'imsi': $scope.search_str.trim()})
            .then($scope.get_results_succeed, $scope.get_results_failed)
    };

    $scope.find_by_mac = function(){
        console.log($scope.search_str.trim());
        $http.post('/api/find_by_mac', {'mac': $scope.search_str.trim()})
            .then($scope.get_results_succeed, $scope.get_results_failed)
    };

}]);